package 算法.leetcode.offer;

/**
 * @author lchenglong
 * @date 2022/3/20
 */
public class Offer58_I {

    public static void main(String[] args) {
        Offer58_I instance = new Offer58_I();
        String s = "  hello world!  ";
        s = instance.reverseWords(s);
        System.out.println("'" +s+"'");
    }

    public String reverseWords(String s) {
        String[] strArr = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int j = strArr.length - 1; j >= 0; j--) {
            if (strArr[j].equals(""))continue;
            if (j == 0)
                sb.append(strArr[j]);
            else
                sb.append(strArr[j]).append(" ");
        }
        return sb.toString().trim();
    }
}
